<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="项目ID" prop="projectId">
        <el-input v-model="queryParams.projectId" placeholder="请输入项目ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="地层ID" prop="stratumId">
        <el-input v-model="queryParams.stratumId" placeholder="请输入地层ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="坐标X" prop="x">
        <el-input v-model="queryParams.x" placeholder="请输入坐标X" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="坐标Y" prop="y">
        <el-input v-model="queryParams.y" placeholder="请输入坐标Y" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="埋深Z" prop="z">
        <el-input v-model="queryParams.z" placeholder="请输入埋深Z" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="备注说明" prop="remark">
        <el-input v-model="queryParams.remark" placeholder="请输入备注说明" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['pile:prodProjectGeology:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['pile:prodProjectGeology:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['pile:prodProjectGeology:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['pile:prodProjectGeology:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="prodProjectGeologyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键ID" align="center" prop="id" /> -->
      <el-table-column label="项目ID" align="center" prop="projectId" />
      <el-table-column label="地层ID" align="center" prop="stratumId" />
      <el-table-column label="坐标X" align="center" prop="x" />
      <el-table-column label="坐标Y" align="center" prop="y" />
      <el-table-column label="埋深Z" align="center" prop="z" />
      <el-table-column label="备注说明" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['pile:prodProjectGeology:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['pile:prodProjectGeology:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改项目地质勘探对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="项目ID" prop="projectId">
          <el-input v-model="form.projectId" placeholder="请输入项目ID" />
        </el-form-item>
        <el-form-item label="上传文件" prop="excelFile">
          <el-upload ref="upload" action="#" :auto-upload="false" :show-file-list="true" :limit="1"
            :before-upload="beforeUpload" :on-change="handleFileChange" :file-list="fileList" accept=".xls,.xlsx" drag>
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <div class="upload-tip">仅支持 .xls, .xlsx 格式文件，文件大小不超过 10MB</div>
          </el-upload>
        </el-form-item>
        <!-- <el-form-item label="地层ID" prop="stratumId">
          <el-input v-model="form.stratumId" placeholder="请输入地层ID" />
        </el-form-item>
        <el-form-item label="坐标X" prop="x">
          <el-input v-model="form.x" placeholder="请输入坐标X" />
        </el-form-item>
        <el-form-item label="坐标Y" prop="y">
          <el-input v-model="form.y" placeholder="请输入坐标Y" />
        </el-form-item>
        <el-form-item label="埋深Z" prop="z">
          <el-input v-model="form.z" placeholder="请输入埋深Z" />
        </el-form-item>
        <el-form-item label="备注说明" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注说明" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProdProjectGeology, getProdProjectGeology, delProdProjectGeology, addProdProjectGeology, updateProdProjectGeology } from "@/api/pile/prodProjectGeology";

export default {
  name: "ProdProjectGeology",
  data() {
    return {
      fileList: [], // 新增
      rawFile: null, // 新增
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目地质勘探表格数据
      prodProjectGeologyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: null,
        stratumId: null,
        x: null,
        y: null,
        z: null,
        remark: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "主键ID不能为空", trigger: "blur" }
        ],
        projectId: [
          { required: true, message: "项目ID不能为空", trigger: "blur" }
        ],
        stratumId: [
          { required: true, message: "地层ID不能为空", trigger: "blur" }
        ],
        x: [
          { required: true, message: "坐标X不能为空", trigger: "blur" }
        ],
        y: [
          { required: true, message: "坐标Y不能为空", trigger: "blur" }
        ],
        z: [
          { required: true, message: "埋深Z不能为空", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "备注说明不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询项目地质勘探列表 */
    getList() {
      this.loading = true;
      listProdProjectGeology(this.queryParams).then(response => {
        this.prodProjectGeologyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        projectId: null,
        stratumId: null,
        x: null,
        y: null,
        z: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目地质勘探";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProdProjectGeology(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目地质勘探";
      });
    },
    handleFileChange(file, fileList) {
      this.fileList = fileList.slice(-1); // 只保留最新的一个
      if (fileList.length > 0) {
        this.rawFile = file.raw;
      } else {
        this.rawFile = null;
      }
    },
    beforeUpload(file) {
      const isExcel =
        file.type === "application/vnd.ms-excel" ||
        file.type ===
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
      const isLt10M = file.size / 1024 / 1024 < 10;
      if (!isExcel) {
        this.$message.error("只能上传 xls/xlsx 文件");
        return false;
      }
      if (!isLt10M) {
        this.$message.error("文件大小不能超过 10MB");
        return false;
      }
      return true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProdProjectGeology(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            if (!this.rawFile) {
              this.$modal.msgError("请上传Excel文件");
              return;
            }

            if (!this.form.projectId) {
              this.$modal.msgError("请选择项目");
              return;
            }
            // 用 FormData 方式上传
            const formData = new FormData();
            formData.append("file", this.rawFile);
            // formData.append("reportDate", this.form.date);
            formData.append("projectId", this.form.projectId);
            addProdProjectGeology(formData).then((response) => {
              this.$modal.msgSuccess("导入成功");
              this.open = false;
              this.getList();
              this.fileList = [];
              this.rawFile = null;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除项目地质勘探编号为"' + ids + '"的数据项？').then(function () {
        return delProdProjectGeology(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('pile/prodProjectGeology/export', {
        ...this.queryParams
      }, `prodProjectGeology_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
/* 拖拽上传区域样式 */
.el-upload-dragger {
  width: 100%;
  height: 180px;
  border: 2px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: border-color 0.3s;
}

.el-upload-dragger:hover {
  border-color: #409eff;
}

.el-upload-dragger.is-dragover {
  border-color: #409eff;
  background-color: #f0f9ff;
}

.el-upload-dragger .el-icon-upload {
  font-size: 67px;
  color: #c0c4cc;
  margin: 40px 0 16px;
  line-height: 50px;
}

.el-upload-dragger .el-upload__text {
  color: #606266;
  font-size: 14px;
  text-align: center;
  margin-bottom: 10px;
}

.el-upload-dragger .el-upload__text em {
  color: #409eff;
  font-style: normal;
  font-weight: bold;
}

.upload-tip {
  font-size: 12px;
  color: #909399;
  text-align: center;
  line-height: 1.5;
}

/* 拖拽悬停状态 */
.el-upload-dragger.is-dragover .el-icon-upload {
  color: #409eff;
}

.el-upload-dragger.is-dragover .el-upload__text {
  color: #409eff;
}

/* 文件列表样式优化 */
.el-upload-list {
  margin-top: 10px;
}

.el-upload-list__item {
  border-radius: 4px;
  border: 1px solid #e4e7ed;
  background-color: #fafafa;
}

.el-upload-list__item:hover {
  background-color: #f0f9ff;
  border-color: #409eff;
}
</style>
